package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

/**
 * 员工管理模块
 */
@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("login")
    public R<Employee> employeeLogin(@RequestBody Employee employee, HttpSession session) {
        // 获取请求登录的账号和密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        // 判断账号或密码是否为空
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return R.error("账号或密码为空，请输入账号和密码");
        }
        // 查询账号是否存在
        Employee e = employeeService.lambdaQuery().eq(Employee::getUsername, username).one();
        if (e == null) {
            return R.error("账号不存在");
        }
        // 判断加密后的密码是否正确
        if (!StringUtils.equals(e.getPassword(), DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)))) {
            return R.error("密码错误");
        }
        // 判断账号是否已被禁用
        if (e.getStatus() != 1) {
            return R.error("账号已被禁用");
        }
        // session中保存用户信息
        session.setAttribute("employee", e);
        return R.success(e);
    }

    /**
     * 员工退出登录
     * @param session
     * @return
     */
    @PostMapping("logout")
    public R<String> employeeLogout(HttpSession session) {
        // 销毁存储用户信息的session对象
        session.invalidate();
        // 响应用户退出的结果
        return R.success("Employee logout success");
    }

    /**
     * 添加员工
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee, HttpSession session) {
        // 获取当前登录的用户对象
//        Employee e = (Employee) session.getAttribute("employee");
        // 设置当前操作人
//        employee.setCreateUser(e.getId());
//        employee.setUpdateUser(e.getId());
        // 设置新增员工的初始化密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        // 保存员工信息
        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    /**
     * 按条件分页查询员工
     * @param curPage
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<Employee>> queryEmployeeByPage(@RequestParam("page") Integer curPage,
                                                 Integer pageSize, String name) {
        // 设置分页参数
        Page<Employee> page = new Page<>(curPage, pageSize);
        // 按分页参数查询
        // 修剪空格和空白字符
//        if (name !=null) {name = name.trim();}
        employeeService.lambdaQuery()
                .like(StringUtils.isNotBlank(name), Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime)
                .page(page);
        return R.success(page);
    }

    /**
     * 修改员工账号
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee, HttpSession session) {
//        Employee e = (Employee) session.getAttribute("employee");
//        employee.setUpdateUser(e.getId());
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据员工id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> queryEmployeeById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

}




